package com.x.crm.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.x.crm.dao.*;
import com.x.crm.model.*;
import com.x.crm.payload.IntentionMsg;
import com.x.crm.rpc.IntentionRpcService;
import com.x.framework.exception.BusinessException;
import com.x.framework.exception.ExceptionCode;
import com.x.framework.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.x.crm.manage.IntentionManage;

import java.util.List;

@Component
@Service(interfaceName = "com.x.crm.rpc.IntentionRpcService", timeout = 60000)
public class IntentionService extends BaseService implements IntentionRpcService {

	@Autowired
	private IntentionDao intentionDao;

	@Autowired
	private FollowUpDao followUpDao;

	@Autowired
	private FollowFixtureDao followFixtureDao;

	@Autowired
	private FollowOrderDao followOrderDao;

	@Autowired
	private FollowReachDao followReachDao;

	@Autowired
	private FollowLoseDao  followLoseDao;

	@Autowired
	private FollowAppointDao  followAppointDao;

	@Autowired
	private OrderCarDao  orderCarDao;

	@Autowired
	private FixtureCarDao  fixtureCarDao;

	@Autowired
	private FollowServiceDao  followServiceDao;





	@Autowired
	private IntentionManage intentionManage;


	public List<Intention> selectIntentionInfoList(Intention intention) {
		try {
			return intentionDao.selectIntentionInfoList(intention);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0003, e);
		}
	}

	public int selectIntentionNum(Intention intention) {
		try {
			return intentionDao.selectIntentionNum(intention);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0003, e);
		}
	}


	/**
	 * 销售意向列表
	 * @param intention
	 * @return
	 */
	public List<Intention> getCustomerIntentionList(Intention intention) {
		try {
			return intentionDao.getCustomerIntentionList(intention);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0003, e);
		}
	}


	/**
	 * 销售意向列表pc
	 * @param intention
	 * @return
	 */
	public List<Intention> getCustomerIntentionListForPc(Intention intention) {
		try {
			switch (intention.getIntentionState()){
				case 20:
					return intentionDao.selectIntentionFollowList(intention,"CRM_FOLLOW_UP","UP_CREATE_TIME");
				case 30:
					return intentionDao.selectIntentionFollowList(intention,"CRM_FOLLOW_APPOINTMENT","APPOINT_CREATE_TIME");
				case 40:
					return intentionDao.selectIntentionFollowList(intention,"CRM_FOLLOW_REACH","REACH_CREATE_TIME");
				case 50:
					return intentionDao.selectIntentionFollowList(intention,"CRM_FOLLOW_ORDER","FOLLOW_ORDER_CREATE_TIME");
				case 60:
					return intentionDao.selectIntentionFollowList(intention,"CRM_FOLLOW_FIXTURE","FOLLOW_FIXTURE_CREATE_TIME");
				case 70:
					return intentionDao.selectIntentionFollowList(intention,"CRM_FOLLOW_LOSE","LOSE_CREATE_TIME");
				default:
					break;
			}
			return null;
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0003, e);
		}
	}

	/**
	 * 销售意向数量PC
	 * @param intention
	 * @return
	 */
	public int getCustomerIntentionNumForPc(Intention intention) {
		try {
			switch (intention.getIntentionState()){
				case 20:
					return intentionDao.selectIntentionFollowNum(intention,"CRM_FOLLOW_UP","UP_CREATE_TIME");
				case 30:
					return intentionDao.selectIntentionFollowNum(intention,"CRM_FOLLOW_APPOINTMENT","APPOINT_CREATE_TIME");
				case 40:
					return intentionDao.selectIntentionFollowNum(intention,"CRM_FOLLOW_REACH","REACH_CREATE_TIME");
				case 50:
					return intentionDao.selectIntentionFollowNum(intention,"CRM_FOLLOW_ORDER","FOLLOW_ORDER_CREATE_TIME");
				case 60:
					return intentionDao.selectIntentionFollowNum(intention,"CRM_FOLLOW_FIXTURE","FOLLOW_FIXTURE_CREATE_TIME");
				case 70:
					return intentionDao.selectIntentionFollowNum(intention,"CRM_FOLLOW_LOSE","LOSE_CREATE_TIME");
				default:
					break;
			}
			return 0;
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0003, e);
		}
	}


	public int getCustomerIntentionNum(Intention intention) {
		try {
			return intentionDao.getCustomerIntentionNum(intention);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0003, e);
		}
	}


	public Intention getCustomerIntentionById(Intention intention) {
		try {
			return intentionDao.getCustomerIntentionById(intention);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0003, e);
		}
	}

	/**
	 * 跟进信息 *******************************
	 * @param followUp
	 * @return
	 */
	public FollowUp insertFollowUp(FollowUp followUp) {
		try {
			return this.insert(followUp);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0003, e);
		}
	}

	public FollowAppoint insertFollowAppoint(FollowAppoint followAppoint) {
		try {
			return this.insert(followAppoint);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0003, e);
		}
	}

	public FollowOrder insertFollowOrder(FollowOrder followOrder) {
		try {
			return intentionManage.insertFollowOrder(followOrder);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0003, e);
		}
	}

	public FollowFixture insertFollowFixture(FollowFixture followFixture) {
		try {
			return intentionManage.insertFollowFixture(followFixture);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0003, e);
		}
	}

	public FollowReach insertFollowReach(FollowReach followReach) {
		try {
			return this.insert(followReach);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0003, e);
		}
	}

	public FollowLose insertFollowLose(FollowLose followLose) {
		try {
			return this.insert(followLose);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0003, e);
		}
	}


	public int updateFollowOrder(FollowOrder followOrder) {
		try {
			return this.update(followOrder);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0003, e);
		}
	}


	public List<FollowUp> selectFollowUpList(FollowUp followUp) {
		try {
			return followUpDao.selectFollowUpList(followUp);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0003, e);
		}
	}

	public List<FollowAppoint> selectFollowAppointList(FollowAppoint followAppoint) {
		try {
			return followAppointDao.selectFollowAppointList(followAppoint);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0003, e);
		}
	}

	public List<FollowOrder> selectFollowOrderList(FollowOrder followOrder) {
		try {
			return followOrderDao.selectFollowOrderList(followOrder);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0003, e);
		}
	}

	public List<FollowReach> selectFollowReachList(FollowReach followReach) {
		try {
			return followReachDao.selectFollowReachList(followReach);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0003, e);
		}
	}

	public List<FollowLose> selectFollowLoseList(FollowLose followLose) {
		try {
			return followLoseDao.selectFollowLoseList(followLose);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0003, e);
		}
	}

	public List<FollowFixture> selectFollowFixtureList(FollowFixture followFixture) {
		try {
			return followFixtureDao.selectFollowFixtureList(followFixture);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0003, e);
		}
	}

	public List<OrderCar> selectOrderCarList(OrderCar orderCar) {
		try {
			return orderCarDao.selectOrderCarList(orderCar);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0003, e);
		}
	}

	@Override
	public List<com.x.crm.model.FollowService> selectServiceList(com.x.crm.model.FollowService followService) {
		try {
			return followServiceDao.selectServiceList(followService);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0003, e);
		}
	}


	public List<FixtureCar> selectFixtureCarList(FixtureCar fixtureCar) {
		try {
			return fixtureCarDao.selectFixtureCarList(fixtureCar);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0003, e);
		}
	}






	/**
	 * *****************************************************
	 * @param intention
	 * @param intentionMsg
	 * @return
	 */







	public Intention insertWxIntentionInfo(Intention intention, IntentionMsg intentionMsg) {
		try {
			return intentionManage.insertWxIntentionInfo(intention, intentionMsg);
//            return this.insert(intention);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0004, e);
		}
	}


	public int updateIntentionInfo(Intention intention) {
		try {
			return intentionManage.updateIntentionInfo(intention);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0005, e);
		}
	}

	public int updateBatch(List<Intention> list) {

		return intentionManage.update(list);
	}

	public int updateIntentionBasicInfo(Intention intention) {
		try {
			return this.update(intention);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0005, e);
		}
	}



	/**
	 * 插入用户及用户意向
	 * @param intention
	 * @return
	 */
	public Intention insertCustomerIntentionInfo(Intention intention) {
		try {
			return intentionManage.insertCustomerIntentionInfo(intention);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0005, e);
		}

	}


	/**
	 * 插入用户及用户意向
	 * @param intention
	 * @return
	 */
	public int updateCustomerIntentionInfo(Intention intention) {
		try {
			return intentionManage.updateCustomerIntentionInfo(intention);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0005, e);
		}

	}


	/**
	 * 基础更新 意向和 customerShop表字段
	 * @param intention
	 * @return
	 */

	public int updateCustomerIntentionBasic(Intention intention) {
		try {
			return intentionManage.updateCustomerIntentionBasic(intention);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0005, e);
		}

	}



}
